package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.Account;
import com.itheima.domain.Infor;
import com.itheima.domain.Trade;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserviceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public Account login(Account account) {
        Account ac = userDao.login(account);
        return ac;
    }

    @Override
    public String selectBalance(String accountID, String password) {
       String remaining =  userDao.selectBalance(accountID,password);
       return remaining;
    }

    @Override
    public List<Trade> selectTransactions(Date startTime, Date endTime,String accountID) {
      /*
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        *//*Date _startTime = null;
        Date _endTime = null;*//*
        List<Trade> trades = null;
        try {
            Date _startTime = sim.parse(startTime);
            Date _endTime = sim.parse(endTime);
            trades = userDao.selectTransactions(_startTime, _endTime, accountID);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return trades;*/
        List<Trade> trades = userDao.selectTransactions(startTime, endTime, accountID);
        for (Trade trade : trades) {
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
            String format = sim.format(trade.getTradeTime());
            trade.setTradeToTime(format);
        }
        return trades;
    }

    @Override
    public Infor transferTo(String accountID, String password, String toAccount, String money) {
       Integer integer =  userDao.selectMoney(accountID);
       Infor infor = new Infor();
        int toMoney = Integer.parseInt(money);
        if (integer<toMoney){
            infor.setFalg(false);
            infor.setError("余额不足");
            return infor;
        }
        Account account = userDao.selectAccountID(toAccount);
        if (account==null){
            infor.setFalg(false);
            infor.setError("账户不存在");
            return infor;
        }
        double charge = toMoney*0.005;
        if (charge<2){
            charge = 2;
        }
        if (charge>20){
            charge = 20;
        }
        userDao.transfer(accountID,-(toMoney+charge));
        userDao.transfer(toAccount,toMoney);
        String digest = "接收账号: "+toAccount;
        userDao.addLog(accountID,"转出",toMoney,new Date(),digest);
        Trade trade = new Trade();
        trade.setAccountID(toAccount);
        trade.setTradeType("转入");
        trade.setTradeMoney(String.valueOf(toMoney));
        trade.setTradeTime(new Date());
        trade.setTradeDigest("输入账号: "+accountID);
        userDao.addTransterLog(trade);
        infor.setFalg(true);
        return infor;
    }
}
